﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.IO;
using System.Windows.Forms;



namespace FrameBuilderLib
{
    public static class LemmAnylser
    {
        //public static string startpath;
        static string searchline = Make_searchline();

// *** Возвращает базовую форму слова
          public static string GetBaseWord(string Word)
          {
              string ResFunc = ReturnMainForm(Word);
              if (ResFunc!=null)
              {
                  //answer := @answers[0];
                  //Result := string(PChar(@answer.m_MainForm));
                  if(ResFunc[0]!='М') return ResFunc;
              }
              return Word;
  
          }

          public static string GetText(string text)
          {
              if (text != null)
              {
                  text = Del_stopwords(text);
                  List<string> words=GetWords(text);
                  text = "";
                  foreach (string word in words)
                  {
                      text += " " + GetBaseWord(word);
                  }
                  text = text.Remove(0, 1);
                  text = Del_stopwords(text);
              }
              return text;
          }

            /// <summary>
            /// удаление стоп-слов
            /// </summary>
            /// <param name="text">текст</param>
            /// <returns>текст без стоп-слов</returns>
          public static string Del_stopwords(string text)
          {
              return Regex.Replace(text, searchline, "",RegexOptions.IgnoreCase);
          }

        /// <summary>
        /// разбор текста на слова
        /// </summary>
        /// <param name="text">текст</param>
        /// <returns>список слов</returns>
          public static List<string> GetWords(string text)
          {
              Regex r = new Regex(@"\w+|%", RegexOptions.IgnoreCase);
              Match words = r.Match(text);
              List<string> res=new List<string>();
              while (words.Success)
              {
                res.Add(words.ToString());
                words = words.NextMatch();
              }
              return res;
          }

          public static bool FindWordInText(List<List<string>> wordlist, string text)
          {
              bool find = true;
              int i=0;
              string searchline = "";
              while (i<wordlist.Count && find)
              {
                  Regex r;
                  foreach (string wl in wordlist[i])
                  {
                      if (wl != "%")
                        searchline += @"|" + @"(\b" + wl + @"\b)";
                      else
                          searchline += @"|" + wl;
                  }
                  searchline = searchline.Remove(0, 1);
                  r = new Regex(searchline, RegexOptions.IgnoreCase);
                  i++;
                  find = r.IsMatch(text);
                  searchline = "";
              }
              return find;
          }

        /// <summary>
        /// сделать поисковую строку, которая ищет стоп-слова
        /// </summary>
        /// <returns></returns>
          public static string Make_searchline()
          {
              string path =Application.StartupPath+ @"\..\..\..\SiteSearch\search_stopwords.txt";
              string text = File.ReadAllText(path, Encoding.Default);
              List<string> stopwords = GetWords(text);
              string my_searchline = "";
              foreach (string sword in stopwords)
              {
                  my_searchline += @"|" + @"(\b" + sword + @"\b)";
              }
              return my_searchline.Remove(0, 1);
          }

        [DllImport("Morflemmdll1.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.None)]
         public static extern string ReturnMainForm(string Word);
         //public static extern Int32 myPuts2(Int32 n);
    }
}
